3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
23 * This class is used to get a list of active users. The ones with specials
24 * rights (sysop, bureaucrat, developer) will have them displayed
25 * next to their names.
29 class ActiveUsersPager
extends UsersPager
{
44 private $blockStatusByUid;
47 * @param IContextSource|null $context
48 * @param FormOptions $opts
50 public function __construct( IContextSource
$context = null, FormOptions
$opts ) {
51 parent
::__construct( $context );
53 $this->RCMaxAge
= $this->getConfig()->get( 'ActiveUserDays' );
54 $this->requestedUser
= '';
56 $un = $opts->getValue( 'username' );
58 $username = Title
::makeTitleSafe( NS_USER
, $un );
59 if ( !is_null( $username ) ) {
60 $this->requestedUser
= $username->getText();
64 $this->groups
= $opts->getValue( 'groups' );
65 $this->excludegroups
= $opts->getValue( 'excludegroups' );
66 // Backwards-compatibility with old URLs
67 if ( $opts->getValue( 'hidebots' ) ) {
68 $this->excludegroups
[] = 'bot';
70 if ( $opts->getValue( 'hidesysops' ) ) {
71 $this->excludegroups
[] = 'sysop';
75 function getIndexField() {
79 function getQueryInfo( $data = null ) {
80 $dbr = $this->getDatabase();
83 $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ) & SCHEMA_COMPAT_READ_NEW
86 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
87 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
88 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
90 // Inner subselect to pull the active users out of querycachetwo
91 $tables = [ 'querycachetwo', 'user' ];
92 $fields = [ 'qcc_title', 'user_id' ];
94 'user' => [ 'JOIN', 'user_name = qcc_title' ],
97 'qcc_type' => 'activeusers',
98 'qcc_namespace' => NS_USER
,
101 if ( $data !== null ) {
102 $options['ORDER BY'] = 'qcc_title ' . $data['order'];
103 $options['LIMIT'] = $data['limit'];
104 $conds = array_merge( $conds, $data['conds'] );
106 if ( $this->requestedUser
!= '' ) {
107 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser
);
109 if ( $this->groups
!== [] ) {
110 $tables['ug1'] = 'user_groups';
111 $jconds['ug1'] = [ 'JOIN', 'ug1.ug_user = user_id' ];
112 $conds['ug1.ug_group'] = $this->groups
;
113 $conds[] = 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
115 if ( $this->excludegroups
!== [] ) {
116 $tables['ug2'] = 'user_groups';
117 $jconds['ug2'] = [ 'LEFT JOIN', [
118 'ug2.ug_user = user_id',
119 'ug2.ug_group' => $this->excludegroups
,
120 'ug2.ug_expiry IS NULL OR ug2.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
122 $conds['ug2.ug_user'] = null;
124 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
125 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
126 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
133 'actor_user = user_id',
135 $fields[] = 'actor_id';
137 $subquery = $dbr->buildSelectSubquery( $tables, $fields, $conds, $fname, $options, $jconds );
139 // Outer query to select the recent edit counts for the selected active users
140 $tables = [ 'qcc_users' => $subquery, 'recentchanges' ];
141 $jconds = [ 'recentchanges' => [ 'LEFT JOIN', [
142 $useActor ?
'rc_actor = actor_id' : 'rc_user_text = qcc_title',
143 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL
), // Don't count wikidata.
144 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE
), // Don't count categorization changes.
145 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
146 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
154 'user_name' => 'qcc_title',
155 'user_id' => 'user_id',
156 'recentedits' => 'COUNT(rc_id)'
158 'options' => [ 'GROUP BY' => [ 'qcc_title' ] ],
160 'join_conds' => $jconds,
164 protected function buildQueryInfo( $offset, $limit, $order ) {
165 $fname = __METHOD__
. ' (' . $this->getSqlComment() . ')';
167 $sortColumns = array_merge( [ $this->mIndexField
], $this->mExtraSortFields
);
168 if ( $order === self
::QUERY_ASCENDING
) {
170 $orderBy = $sortColumns;
171 $operator = $this->mIncludeOffset ?
'>=' : '>';
175 foreach ( $sortColumns as $col ) {
176 $orderBy[] = $col . ' DESC';
178 $operator = $this->mIncludeOffset ?
'<=' : '<';
180 $info = $this->getQueryInfo( [
181 'limit' => intval( $limit ),
184 $offset != '' ?
[ $this->mIndexField
. $operator . $this->mDb
->addQuotes( $offset ) ] : [],
187 $tables = $info['tables'];
188 $fields = $info['fields'];
189 $conds = $info['conds'];
190 $options = $info['options'];
191 $join_conds = $info['join_conds'];
192 $options['ORDER BY'] = $orderBy;
193 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
196 protected function doBatchLookups() {
197 parent
::doBatchLookups();
200 foreach ( $this->mResult
as $row ) {
201 $uids[] = $row->user_id
;
203 // Fetch the block status of the user for showing "(blocked)" text and for
204 // striking out names of suppressed users when privileged user views the list.
205 // Although the first query already hits the block table for un-privileged, this
206 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
207 $dbr = $this->getDatabase();
208 $res = $dbr->select( 'ipblocks',
209 [ 'ipb_user', 'MAX(ipb_deleted) AS deleted, MAX(ipb_sitewide) AS sitewide' ],
210 [ 'ipb_user' => $uids ],
212 [ 'GROUP BY' => [ 'ipb_user' ] ]
214 $this->blockStatusByUid
= [];
215 foreach ( $res as $row ) {
216 $this->blockStatusByUid
[$row->ipb_user
] = [
217 'deleted' => $row->deleted
,
218 'sitewide' => $row->sitewide
,
221 $this->mResult
->seek( 0 );
224 function formatRow( $row ) {
225 $userName = $row->user_name
;
227 $ulinks = Linker
::userLink( $row->user_id
, $userName );
228 $ulinks .= Linker
::userToolLinks( $row->user_id
, $userName );
230 $lang = $this->getLanguage();
234 $ugms = self
::getGroupMemberships( intval( $row->user_id
), $this->userGroupCache
);
235 foreach ( $ugms as $ugm ) {
236 $list[] = $this->buildGroupLink( $ugm, $userName );
239 $groups = $lang->commaList( $list );
241 $item = $lang->specialList( $ulinks, $groups );
243 // If there is a block, 'deleted' and 'sitewide' are both set on
244 // $this->blockStatusByUid[$row->user_id].
246 $isBlocked = isset( $this->blockStatusByUid
[$row->user_id
] );
248 if ( $this->blockStatusByUid
[$row->user_id
]['deleted'] == 1 ) {
249 $item = "<span class=\"deleted\">$item</span>";
251 if ( $this->blockStatusByUid
[$row->user_id
]['sitewide'] == 1 ) {
252 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
255 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits
)
256 ->params( $userName )->numParams( $this->RCMaxAge
)->escaped();
258 return Html
::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );